import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:workmate/provider/global.dart';
import 'package:workmate/provider/theme.dart';
import 'package:workmate/routers/pages.dart';
import 'package:workmate/widget/back.dart';
import 'package:sp_util/sp_util.dart';

class HomePage extends StatefulWidget {
  final Map<String, List<String>> query;
  const HomePage(this.query, {Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePage();
}

class _HomePage extends State<HomePage> {
  final int _sharedCounter = SpUtil.getInt('counter') ?? 0;
  int _counter = SpUtil.getInt('counter') ?? 0;
  String? user;
  String? params;
  late Map<String, dynamic> config;
  void _incrementCounter() {
    setState(() {
      _counter++;
      SpUtil.putInt('counter', _counter);
    });
  }

  @override
  void initState() {
    super.initState();
    user = widget.query['user']?.first;
    // user = widget.query['user']?.join(',');
    params = widget.query['params']?.join(',');
    config = APP.config.toJson();
  }

  @override
  Widget build(BuildContext context) {
    ThemeProvider theme = Provider.of<ThemeProvider>(context);
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('title').tr(),
        centerTitle: true,
        leadingWidth: 44,
        leading: const BackWidget(),
      ),
      body: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text(
          'home.counter',
          style: TextStyle(color: theme.primaryColor, fontSize: 24),
        ).tr(namedArgs: {'counter': '$_counter shared: $_sharedCounter'}),
        Text(
          'home.query',
          maxLines: 5,
          textAlign: TextAlign.center,
          style: TextStyle(color: theme.primaryColor, fontSize: 24),
        ).tr(args: ['$user', '[$params]']),
        Text(
          'home.config',
          style: TextStyle(color: theme.primaryColor, fontSize: 24),
        ).tr(args: ['$config']),
        GestureDetector(
            onTap: () {
              APP.router.goto(Pages.web, query: {
                'url': 'http://localhost:8514/getconfig',
              });
            },
            child: Text(
              'home.web',
              style: TextStyle(color: theme.primaryColor, fontSize: 24),
            ).tr(args: ['goto'])),
      ]),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
